package com.cuitrip.business.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.cuitrip.business.login.model.ThirdAccount;
import com.cuitrip.business.login.proxy.LoginProxy;
import com.cuitrip.business.user.model.CtUserInfo;
import com.cuitrip.service.R;
import com.cuitrip.util.track.TrackScreenName;
import com.cuitrip.util.track.TrackUtil;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.lab.jumper.Jumper;
import com.lab.utils.ToastUtil;
import com.lab.utils.Utils;

import butterknife.ButterKnife;
import butterknife.Bind;

/**
 * Created by sheena on 15/12/18.
 */
public class BindEmailActivity extends CustomUiFragmentActivity{

    @Bind(R.id.et_email)
    EditText emailAccout;

    private ThirdAccount thirdAccount;

    public static final String OBJECT_KEY = "object_key";
    public static final int REQUESET_CODE_BINDEMAIL=0x01;

    public static void lauchBindEmailActivity(Activity activity, ThirdAccount thirdAccount) {
        Intent intent = new Intent(activity, BindEmailActivity.class);
        intent.putExtra(OBJECT_KEY, thirdAccount);
        Jumper.launchActivityForResult(activity, intent, REQUESET_CODE_BINDEMAIL);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent.hasExtra(OBJECT_KEY)) {
            thirdAccount = (ThirdAccount) intent.getSerializableExtra(OBJECT_KEY);
        } else {
            finish();
        }
        super.onCreate(savedInstanceState, R.layout.activity_bind_email);
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
    }

    @Override
    public void initListener() {
        mTopbarRightArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isValid()){
                    showLoading();
                    //发送请求
                    LoginProxy.getInstance().thirdLogin(thirdAccount,new LoginProxy.CallBack(){

                        @Override
                        public void onLoginSuccess(Object data) {
                            if (data instanceof CtUserInfo){
                                CtUserInfo userInfo = (CtUserInfo) data;
                                hideLoading();
                                LoginProxy.getInstance().reloginWithRestartActivity(BindEmailActivity.this);
                            }
                            setResult(RESULT_OK);
                            finish();
                        }

                        @Override
                        public void onLoginFail(int resultCode, String message) {
                            hideLoading();
                            if (!TextUtils.isEmpty(message)){
                                ToastUtil.toast(message);
                            }

                        }
                    },emailAccout.getText().toString());
                }
            }
        });
    }

    /* @OnClick(R.id.bind_action)
    public void bindAction(){
        if(isValid()){
            showLoading();
        }

        //发送请求
        //LoginProxy.getInstance().thirdLogin();

    }*/

    private boolean isValid() {
        if (TextUtils.isEmpty(emailAccout.getText())) {
            Utils.shakeView(emailAccout);
            return false;
        }
        if(!Utils.isEmail(emailAccout.getText().toString())){
            ToastUtil.toast(BindEmailActivity.this, getString(R.string.mail_limit), 0);
            return false;
        }

        return true;
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config=new CustomUiConfig();
        config.mTopbarLeftAreaName=getString(R.string.back_icon);
        config.mTopbarMiddleAreaName = getString(R.string.mail);
        config.mTopbarRightAreaName=getString(R.string.operation_ok_1);
        config.mRightStyle= CustomUiConfig.AreaStyle.TEXT_STYLE;
        return config;
    }

    @Override
    public void onTopbarLeftPress() {
        //FIXME @sheena 需要修改
        //LoginProxy.getInstance().gotoUserEnter(BindEmailActivity.this);
        finish();
    }

    @Override
    public void onBackPressed() {
        //FIXME @sheena 需要修改
       // LoginProxy.getInstance().gotoUserEnter(BindEmailActivity.this);
        finish();
    }
}
